<?php
namespace Isas\Com\Indicator;

/**
 * 指标抽象支持类
 * @author w.
 *
 */
abstract class AbstractIndicatorSupport extends AbstractIndicatorBaseSupport {
	
    // 保存指标值
    protected function setBarData($key,$val,$bars,$shift){
        if(!array_key_exists(self::SUPPLY_BAR_TAG,$bars[$shift])){
            $bars[$shift][$key] = $val;
            $bars[$shift][self::CALC_BAR_TAG] = true;
        }
        return $bars;
    }
    
	// 保存到数据库
	protected function saveDB($key,$bar){
	    if(array_key_exists(self::CALC_BAR_TAG,$bar) && array_key_exists($key,$bar)){
	        $val = $bar[$key];
	        $doc = array('$set'=>array($key=>$val));
	        $where = array('code'=>$bar['code'],'index'=>$bar['index']);
	        $this->getCollection()->update($where,$doc);
	        return 1;
	    }
	    return 0;
	}
	
	protected function getSupplyBars($bars,$period){
	    $colName = $this->collection->getName();
	    $year = (int)explode("_",$colName)[0];    
	    $prevYearCol = ($year-1).'_'.explode("_",$colName)[1];
	    $prevYearCol = $this->dbClient->{$prevYearCol};
	    $code = $bars[0]['code'];
	    $query = array('code'=>$code);
	    $datas = $prevYearCol->find($query)->sort(array('index'=>-1))->limit($period);
	    $_bars = null;
	    if($datas->count()>0){
	        $preBars = array();
	        $k = 0;
	        foreach ($datas as $val) {
	            $val[self::SUPPLY_BAR_TAG] = true;
	            $preBars[$k] = $val;
	            $k++;
	        }
	        $preBars = array_reverse($preBars);
	        $_bars = array_merge($preBars,$bars);
	        $bars = $_bars;
	    }
	    return $bars;
	}

	private function __getUpdateLastBar($key,$code){
	    $lastBarIndex = 0;
	    $query = array('code'=>$code,$key=>array('$gt'=>0));
	    $data = $this->collection->find($query)->sort(array('index'=>-1))->limit(1);
	    if($data->count()>0){
	        foreach ($data as $val) {
	            $lastBarIndex = $val['yearIndex'];
	        }
	    }
	    return $lastBarIndex;
	}
	
	protected function getCalcLastBar($key,$bars){
	    $cnt = count($bars);
	    for($i=0;$i<$cnt;$i++){
	        
	    }
	}
	
	// 获取记录键值
	private function getUpdateLogVersionKey($key){
		return $this->getUpdateLogCollectionName().'_'.$key;
	}
	
	// 获取集合名称
	private function getUpdateLogCollectionName(){
		return $this->getCollection()->getName();
	}
	
	// 获取更新记录表的集合对象
	private function getUpdateLogCollection(){
		return getIsasDB()->{'update_log'};
	}
	

}

?>
